package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.OrderDetailDto;
import io.geekidea.boot.shop.entity.OrderDetail;
import io.geekidea.boot.shop.mapper.OrderDetailMapper;
import io.geekidea.boot.shop.query.app.AppOrderDetailQuery;
import io.geekidea.boot.shop.query.web.OrderDetailQuery;
import io.geekidea.boot.shop.service.base.api.OrderDetailService;
import io.geekidea.boot.shop.vo.app.AppOrderDetailVo;
import io.geekidea.boot.shop.vo.web.OrderDetailVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService {

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addOrderDetail(OrderDetailDto dto) {
        OrderDetail orderDetail = new OrderDetail();
        BeanUtils.copyProperties(dto, orderDetail);
        return save(orderDetail);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateOrderDetail(OrderDetailDto dto) {
        Integer id = dto.getId();
        OrderDetail orderDetail = getById(id);
        if (orderDetail == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, orderDetail);
        return updateById(orderDetail);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteOrderDetail(Integer id) {
        return removeById(id);
    }

    @Override
    public OrderDetailVo getOrderDetailById(Integer id) {
        return orderDetailMapper.getOrderDetailById(id);
    }

    @Override
    public Paging<OrderDetailVo> getOrderDetailPage(OrderDetailQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<OrderDetailVo> list = orderDetailMapper.getOrderDetailPage(query);
        Paging<OrderDetailVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppOrderDetailVo getAppOrderDetailById(Integer id) {
        return orderDetailMapper.getAppOrderDetailById(id);
    }

    @Override
    public Paging<AppOrderDetailVo> getAppOrderDetailPage(AppOrderDetailQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppOrderDetailVo> list = orderDetailMapper.getAppOrderDetailPage(query);
        Paging<AppOrderDetailVo> paging = new Paging<>(list);
        return paging;
    }

}
